﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SGP;

namespace TileEditor
{
    public partial class TileSetPallete : Form
    {
        CSGP_Direct3D DX = CSGP_Direct3D.GetInstance();
        CSGP_TextureManager TM = CSGP_TextureManager.GetInstance();

        TileSet[] tileSets;
        Form1 f;
        Rectangle currSelect;
        Rectangle actualSelect;

        bool selecting;
        public TileSetPallete()
        {
            InitializeComponent();
            DX.AddRenderTarget(TileSetPalleteView);
            currSelect = new Rectangle(-1, -1, -1, -1);
            currSelect = f.GetSelection();
        }

        public TileSetPallete(Form1 ownedby)
        {
            InitializeComponent();
            DX.AddRenderTarget(TileSetPalleteView);
            currSelect = new Rectangle(-1, -1, -1, -1);
            actualSelect = currSelect;
            f = ownedby;
            selecting = new bool();
            selecting = false;
        }

        public void UpdateTileSets()
        {
            tabControlTileSets.TabPages.Clear();

            tileSets = new TileSet[f.GetLevel().TileSets.Length];
            tileSets = f.GetLevel().TileSets;

            for (int i = 0; i < tileSets.Length; i++)
            {
                string label = "TileSet" + (i + 1).ToString();
                tabControlTileSets.TabPages.Add(label, label);
            }

            AdjustScrollSize();
        }

        public void Render()
        {
            DX.Clear(TileSetPalleteView, Color.White);
            DX.DeviceBegin();
            DX.SpriteBegin();

            if(f.GetLevel().TileSets[f.GetLevel().CurrTileSet].Id != -1)
                TM.Draw(f.GetLevel().TileSets[f.GetLevel().CurrTileSet].Id, TileSetPalleteView.AutoScrollPosition.X, TileSetPalleteView.AutoScrollPosition.Y);

            Point defaultStart = new Point(0, 0);
            Point offset = Point.Empty;
            offset.X = defaultStart.X + TileSetPalleteView.AutoScrollPosition.X;
            offset.Y = defaultStart.Y + TileSetPalleteView.AutoScrollPosition.Y;

            int width = f.GetLevel().TileSize.Width;
            int height = f.GetLevel().TileSize.Height;

            // Iterate trhough all the tiles in the map
            for (int x = 0; x < f.GetLevel().TileSets[f.GetLevel().CurrTileSet].SetSize.Width; x++)
            {
                for (int y = 0; y < f.GetLevel().TileSets[f.GetLevel().CurrTileSet].SetSize.Height; y++)
                {
                    // Calculate where the tile is going to be drawn in the map.
                    Rectangle destRect = Rectangle.Empty;
                    destRect.X = x * width + offset.X;
                    destRect.Y = y * height + offset.Y;
                    destRect.Size = f.GetLevel().TileSize;

                    DX.DrawHollowRect(destRect, Color.Black, 1);
                }
            }


            if (currSelect.Left != -1 && currSelect.Top != -1 && currSelect.Width != -1 && currSelect.Height != -1)
            {
                if (!selecting)
                {
                    Rectangle render = new Rectangle(currSelect.X + TileSetPalleteView.AutoScrollPosition.X, currSelect.Y + TileSetPalleteView.AutoScrollPosition.Y, currSelect.Width, currSelect.Height);
                    DX.DrawHollowRect(render, Color.Red, 2);
                }
                else
                {
                    Rectangle render = new Rectangle(actualSelect.X + TileSetPalleteView.AutoScrollPosition.X, actualSelect.Y + TileSetPalleteView.AutoScrollPosition.Y, actualSelect.Width, actualSelect.Height);
                    DX.DrawHollowRect(render, Color.Red, 2);
                }
            }

            DX.SpriteEnd();
            DX.DeviceEnd();


            DX.Present();
        }

        private void tabControlTileSets_SelectedIndexChanged(object sender, EventArgs e)
        {
            f.SetSelectedTileSet(tabControlTileSets.SelectedIndex);
            AdjustScrollSize();

        }

        public void AddTileSet()
        {
            string label = "TileSet" + (f.GetLevel().TileSets.Length).ToString();
            tabControlTileSets.TabPages.Add(label, label);
        }

        public void RemoveTileSet()
        {
            tabControlTileSets.TabPages.RemoveAt(tabControlTileSets.TabPages.Count - 1);

        }

        private void TileSetPalleteView_Resize(object sender, EventArgs e)
        {
            DX.Resize(TileSetPalleteView, TileSetPalleteView.ClientSize.Width, TileSetPalleteView.ClientSize.Height, true);
        }

        public void AdjustScrollSize()
        {
            int width = (int)f.GetLevel().TileSets[f.GetLevel().CurrTileSet].SetSize.Width * (int)f.GetLevel().TileSize.Width;
            int height = (int)f.GetLevel().TileSets[f.GetLevel().CurrTileSet].SetSize.Height * (int)f.GetLevel().TileSize.Height;
            TileSetPalleteView.AutoScrollMinSize = new Size(width, height);
        }



        private void TileSetPalleteView_MouseDown(object sender, MouseEventArgs e)
        {
            Point defaultStart = e.Location;
            Point offset = Point.Empty;
            offset.X = defaultStart.X - TileSetPalleteView.AutoScrollPosition.X;
            offset.Y = defaultStart.Y - TileSetPalleteView.AutoScrollPosition.Y;

            if (e.Button == MouseButtons.Left && selecting == false)
            {
                selecting = true;
                int width = f.GetLevel().TileSize.Width;
                int height = f.GetLevel().TileSize.Height;

                int x = (offset.X / width) * width;
                int y = (offset.Y / height) * height;

                currSelect = new Rectangle(x, y, width, height);
                actualSelect = currSelect;
            }
        }

        private void TileSetPalleteView_MouseUp(object sender, MouseEventArgs e)
        {
            Point defaultStart = e.Location;
            Point offset = Point.Empty;
            offset.X = defaultStart.X - TileSetPalleteView.AutoScrollPosition.X;
            offset.Y = defaultStart.Y - TileSetPalleteView.AutoScrollPosition.Y;
            if (e.Button == MouseButtons.Left)
            {

                selecting = false;
                int width = f.GetLevel().TileSize.Width;
                int height = f.GetLevel().TileSize.Height;

                int x = (offset.X / width) * width;
                int y = (offset.Y / height) * height;

                if (x < currSelect.X)
                    currSelect = new Rectangle(x, currSelect.Y, currSelect.X - x + width, currSelect.Height);
                else if (x > currSelect.X)
                    currSelect = new Rectangle(currSelect.X, currSelect.Y, x - currSelect.X + width, currSelect.Height);

                if (y < currSelect.Y)
                    currSelect = new Rectangle(currSelect.X, y, currSelect.Width, currSelect.Y - y + height);
                else if (y > currSelect.Y)
                    currSelect = new Rectangle(currSelect.X, currSelect.Y, currSelect.Width, y - currSelect.Y + height);

                f.SetSelection(currSelect);
            }
        }

        private void TileSetPalleteView_MouseMove(object sender, MouseEventArgs e)
        {
            Point defaultStart = e.Location;
            Point offset = Point.Empty;
            offset.X = defaultStart.X - TileSetPalleteView.AutoScrollPosition.X;
            offset.Y = defaultStart.Y - TileSetPalleteView.AutoScrollPosition.Y;
            if (e.Button == MouseButtons.Left)
            {
                int width = f.GetLevel().TileSize.Width;
                int height = f.GetLevel().TileSize.Height;

                int x = (offset.X / width) * width;
                int y = (offset.Y / height) * height;

                actualSelect = currSelect;
               

                if (y < currSelect.Y)
                    actualSelect = new Rectangle(actualSelect.X, y, actualSelect.Width, currSelect.Y - y + height);
                else if (y > currSelect.Y)
                    actualSelect = new Rectangle(actualSelect.X, currSelect.Y, actualSelect.Width, y - currSelect.Y + height);
        

                if (x < currSelect.X)
                    actualSelect = new Rectangle(x, actualSelect.Y, currSelect.X - x + width, actualSelect.Height);
                else if (x > currSelect.X)
                    actualSelect = new Rectangle(currSelect.X, actualSelect.Y, x - currSelect.X + width, actualSelect.Height);
     

                if (x < currSelect.X && y < currSelect.Y)
                    actualSelect = new Rectangle(x, y, currSelect.X - x + width, currSelect.Y - y + height);
                else if ( x > currSelect.X && y > currSelect.Y)
                    actualSelect = new Rectangle(currSelect.X, currSelect.Y, x - currSelect.X + width, y - currSelect.Y + height);

                if (x < currSelect.X && y > currSelect.Y)
                    actualSelect = new Rectangle(x, currSelect.Y, currSelect.X - x + width, y - currSelect.Y + height);
                else if (x > currSelect.X && y < currSelect.Y)
                    actualSelect = new Rectangle(currSelect.X, y, x - currSelect.X + width, currSelect.Y - y + height);

            }

            AdjustScrollSize();
        }

    }
}
